//
//  GeneralModelTableViewController+Associated.m
//  NongWanCloud
//
//  Created by YueWen on 2018/7/5.
//  Copyright © 2018年 YueWen. All rights reserved.
//

#import "NWTableViewController+Associated.h"

@implementation NWTableViewController (Associated)

- (void)setAssociate:(BOOL)associate
{
    objc_setAssociatedObject(self, @selector(isAssociate), @(associate), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}


- (BOOL)isAssociate
{
    NSNumber *isAss = objc_getAssociatedObject(self, _cmd);
    
    if (!isAss) { return false; }
    
    return isAss.boolValue;
}


- (void)setCanScroll:(BOOL)canScroll
{
    objc_setAssociatedObject(self, @selector(canScroll), @(canScroll), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}


- (BOOL)canScroll
{
    NSNumber *canScroll = objc_getAssociatedObject(self, _cmd);
    
    if (!canScroll) { return false; }
    
    return canScroll.boolValue;
}


- (void)refresh {
    [self headerRefreshBeginHandler];
}


#pragma mark - RITLAssociatePageContentController
- (void)contentControllerShouldScroll:(BOOL)canScroll
{
    self.canScroll = canScroll;
    
    if (!canScroll) {//如果不能滚动，归位
        self.tableView.ritl_contentOffSetX = -1 * self.tableView.contentInset.top;
    }
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (!self.associate) { return; }
    
    if (!self.canScroll) {//不能滚动，保持不滚动
        scrollView.contentOffset = CGPointMake(0, -1 * scrollView.contentInset.top);
         [[NSNotificationCenter defaultCenter] postNotificationName:RITLAssocicateViewScrollEnableNotificationName object:self];//外界可以滚动
    }
    
    else if (scrollView.ritl_contentOffSetY <= 0) {
        
        self.canScroll = false;
        scrollView.contentOffset = CGPointMake(0, -1 * scrollView.contentInset.top);
        
        //告诉外界滚动视图可以进行滚动
        [[NSNotificationCenter defaultCenter] postNotificationName:RITLAssocicateViewLeaveToTopNotificationName object:self];
    }
    
    else if(scrollView.ritl_contentOffSetY > 0) {//外界需要固定住，自己滚动即可
        
        if (scrollView.ritl_contentOffSetY > self.view.bounds.size.height / 2.0){
            [[NSNotificationCenter defaultCenter] postNotificationName:RITLAssocicateViewScrollNotEnableNotificationName object:self];//外界不可以滚动
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:RITLAssocicateViewScrollingNotificationName object:self];
    }
}


@end
